using System;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ColourSpecifications
{
    internal class FailureCountMetric : MetricBase
    {
        public static readonly IMetric Instance = new FailureCountMetric();

        private FailureCountMetric()
        {
        }

        public override string NameSingular
        {
            get { return "failure"; }
        }

        public override string NamePlural
        {
            get { return "failures"; }
        }

        public override Func<IMonitorState, decimal?> GetValueFunction
        {
            get{return i => i.FailureCount;}
        }

        public override bool IsValidValue(decimal value, out string message)
        {
            if(Math.Abs(value - Math.Truncate(value)) > 0.0001m)
            {
                message = "Must be a whole number";
                return false;
            } 

            if (value > MonitorState.MaximumStoredFailureCount)
            {
                message = string.Format("Cannot be more than {0}. This is the number of errors stored", MonitorState.MaximumStoredFailureCount);
                return false;
            }
            message = null;
            return true;
        }
    }
}